<?php
namespace app\api\controller;
/*首页控制器*/

use think\facade\Db;
use think\facade\Event;
use think\Request;
use app\BaseController;
use app\common\controller\ApiController;
use think\App;
use Swoole\Coroutine\Channel;
use \think\Swoole\WebSocket\Server;
use think\swoole\coroutine\Context;
use app\common\service\Redis as RedisServer;
class Index extends ApiController
{

    public function initialize()
    {
        parent::initialize();
        $this->advert =   app('app\common\model\banner\Advert');
        $this->article =  app('app\common\model\media\Articles');
        $this->configuration =  app('app\common\model\system\Configuration');
        $this->store =  app('app\common\model\mall\Store');
        $this->account =  app('app\common\model\mall\Account');
        $this->see_history =  app('app\common\model\system\SeeHistory');
        $this->cate =  app('app\common\model\common\Categories');
        $this->collect =  app('app\common\model\common\Collect');
        $this->user =  app('app\common\model\user\WxUsers');
        $this->user_net =  app('app\common\model\media\UserNet');
        $this->topic =  app('app\common\model\media\Topic');
        $this->order =  app('app\common\model\mall\Order');
        $this->redis =  app('app\common\subscribe\Subscribe');
        $this->group_master =  app('app\common\model\mall\GroupMaster');
        $this->group_join =  app('app\common\model\mall\GroupJoin');
        $this->chat_message = app('app\common\model\chat\ChatMessage');

    }

    /**
     * 首页
     * @param Request $request
     * @return \think\response\Json
     */
    public function index(Request $request,\think\swoole\Manager $manager)
    {
        /*$redis = Redis::instance(0);
        $redis->hSet('user:1', 'userName', 'admin');
        //$redis = app('app\common\subscribe\Subscribe');
        $redis->setex('order',20,'2017401');*/
        //$redis->psubscribe();

        //dd($manager);
        /*
                $manager->getServer()->task('app\common\listener\EmailTask',1,function (\Swoole\Server $server, $task_id, $data) {
                    echo "Task Callback: ";
                    var_dump($task_id, $data);
                });*/
        switch($request->param('cate')){//1：首页轮播图；2：新闻动态轮播图；3：校园风采轮播图
            case 1:$ad = $this->advert->field('id,type as types,thumb,bind_id')->where('position_id',1)->order('sort','asc')->select();
                //首页推荐资讯
                $artilce = $this->article->where(['is_top'=>1,'status'=>1,'type'=>1])->orderRaw('rand()')->limit(3)->select();

                foreach($artilce as $k=>$v){

                    !empty($v['image']) && $artilce[$k]['images'] = $v['image'];
                }
                break;
            case 2:$ad = $this->advert->field('id,type as types,thumb,bind_id')->where('position_id',2)->order('sort','asc')->select();
                //首页推荐资讯
                $artilce = $this->article->where(['is_top'=>1,'status'=>1,'type'=>1])->orderRaw('rand()')->limit(3)->select();

                foreach($artilce as $k=>$v){

                    !empty($v['image']) && $artilce[$k]['images'] = $v['image'];
                }
                break;
            case 3:$ad = $this->advert->field('id,type as types,thumb,bind_id')->where('position_id',3)->order('sort','asc')->select();
                //机构新闻
                $artilce =$this->article->where(['is_top'=>1,'status'=>1,'type'=>2])->orderRaw('rand()')->limit(3)->select();
                foreach($artilce as $k=>$v){

                    !empty($v['image']) && $artilce[$k]['images'] = $v['image'];
                };break;
        }
        $data['admin_id'] = 8;
        $data['admin_avatar_url'] = 'http://bgt-param.oss-cn-hangzhou.aliyuncs.com/upload/20200721/49640565c5f8400385e46403877181269a063d0a.png';
        //$data['user_id'] = $request->user_id;

        /* $data['user_avatar'] = $this->user->where('id',$request->user_id)->value('avatar_url');
         $data['user_name'] = $this->user->where('id',$request->user_id)->value('nick_name');*/
        //判断直播是否开启
        $show_status = $this->configuration->withoutGlobalScope()->find(20);
        return json(['code'=>200,'msg'=>'获取成功','data'=>['info'=>$data,'show_status'=>$show_status->val,'article'=>$artilce,'ad'=>$ad]]);
    }

    /**
     * 机构列表.
     * @cate  1：限时抢购；2：艺术类；3：文化类’；4：成人教育;5:早教类;
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $model = $this->store;
        $model = $model->where('status',1);
        switch($request->param('cate')){
            case 1: $model = $model->where('is_sale',1);break;
            case 2: $ca = 16;$model = $model->where('category_id',16);break;
            case 3: $ca = 1;$model = $model->where('category_id',1);break;
            case 4: $model = $model->where('category_id',18);break;
            case 5: $ca = 17;$model = $model->where('category_id',17);break;
            //case 6: $model = $model->where('is_top',1);break;
        }
        if($request->param('name')){
            $model = $model->where('name','like','%'.$request->param('name').'%');
        }

        if($request->param('state') == 5) { //排序

            /* $list =  mongo('second')
                ->limit(5)
                ->select();

            //dd($list);
            mongo('second')->insert([
                'from' => '粥',
                'from_name' =>'政策',

            ]);
            dd(222);*/
            /*$redis = RedisServer::instance(2);
            $res = $model->where('category_id',17)->order('sort', 'desc')->column('id');
            foreach($res  as $k=>$v){
                $redis->lpush('RmtStoreOrder17',$v);
            };
            //$redis->Rpush('RmtStoreOrder',$redis->Lpop('RmtStoreOrder'));
            $stor = $redis->lrange('RmtStoreOrder17',0,100);

            //$this->chat =   app('app\common\model\chat\Chat');
            //$this->system_admin =  app('app\common\model\system\SystemAdmin');
            $sto = implode(',',$stor);

            $model->whereIn('id',$sto)
                ->orderRaw("field(id,$sto)")
                //->order("field(id,$sto)")
                ->select()->toArray();*/
            //dd($model);
            //已经拥有的客户端fd


        }
        if($request->param('cate') == ''){ //排序
            $redis = RedisServer::instance(2);
            $stor = $redis->lrange('RmtStoreOrder',0,100);

            $sto = implode(',',$stor);

            $data = $model->whereIn('id',$sto)
                ->orderRaw("field(id,$sto)")
                ->select();
            //$redis->rpush('RmtStore',$redis->Lpop('RmtStore'));

            $data = [
                'data'      => $data         // 分页数据
            ];
        }elseif($request->param('cate') == 1){

            $res = $model->field('id,thumb,name,star,description')->order('sort','asc')->paginate($request->param('limit',10));

            $data = [
                'total'     => $res->total(),         // 总记录数
                'current_page'       => $res->currentPage(),   // 当前页码
                'per_page'      => $res->listRows(),      // 每页记录数
                'data'      => $res->items()          // 分页数据
            ];
        }else{

            $redis = RedisServer::instance(2);

            $stor = $redis->lrange('RmtStoreOrder'.$ca,0,100);

            //$this->chat =   app('app\common\model\chat\Chat');
            //$this->system_admin =  app('app\common\model\system\SystemAdmin');
            $sto = implode(',',$stor);

            $data = $model->whereIn('id',$sto)
                ->orderRaw("field(id,$sto)")
                //->order("field(id,$sto)")
                ->select()->toArray();

            $data = [
                'data'      => $data         // 分页数据
            ];
        }


        return json(['code'=>200,'msg'=>'获取成功','data'=>$data]);
    }


    /**
     * 课程列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function rand_account(Request $request)
    {
        $res = $this->account->orderRaw('rand()')->limit(2)->select();

        return json(['code'=>200,'msg'=>'获取成功','data'=>$res]);
    }
    /*首页启动页*/
    public function start(Request $request){
        $res = $this->advert->where('position_id',4)->orderRaw('rand()')->find();

        return json(['code'=>200,'msg'=>'获取成功','data'=>$res]);
    }
    /*网上课程观看记录*/
    public function see_history(Request $request){

        try{
            if($this->see_history->where(['account_id'=>$request->param('account_id'),
                'item_id'=>$request->param('item_id'),
                'user_id'=>$request->user_id])->find()){
                $this->see_history->where(['account_id'=>$request->param('account_id'),
                    'item_id'=>$request->param('item_id'),
                    'user_id'=>$request->user_id])->update(['is_all'=>$request->param('is_all','0'),'time'=>$request->param('time')]);
            }else{
                $this->see_history->create(['account_id'=>$request->param('account_id'),
                    'item_id'=>$request->param('item_id'),
                    'user_id'=>$request->user_id,'time'=>$request->param('time'),'is_all'=>$request->param('is_all','0')]);
            }
            return json(['code'=>200,'msg'=>'记录成功']);
        }catch(\Exception $e){
            return json(['code'=>202,'msg'=>'记录失败']);
        }


    }

    /*搜索机构和课程*/
    public function search_item(Request $request){

        //state :1:热门；0：普通
        switch($request->param('type')){//1:网上课程 ；2：线下课程  ；3：机构 4://资讯
            case 1: $model = $this->account;
                $model = $model->where(['status'=>1,'type'=>1]);
                if($request->param('name')){
                    $model = $model->where('title','like','%'.$request->param('name').'%');
                };
                $res = $model->paginate($request->param('limit',10));
                $data = [
                    'total'     => $res->total(),         // 总记录数
                    'current_page'       => $res->currentPage(),   // 当前页码
                    'per_page'      => $res->listRows(),      // 每页记录数
                    'data'      => $res->items()          // 分页数据
                ];
                break;
            case 2:
                $store_ids = $this->store->where('status',1)->column('id');

                $model = $this->account;
                $model = $model->where(['status'=>1,'type'=>2])->whereIn('store_id',$store_ids);
                if($request->param('name')){
                    $model = $model->where('title','like','%'.$request->param('name').'%');
                };
                $res = $model->paginate($request->param('limit',10));
                $data = [
                    'total'     => $res->total(),         // 总记录数
                    'current_page'       => $res->currentPage(),   // 当前页码
                    'per_page'      => $res->listRows(),      // 每页记录数
                    'data'      => $res->items()          // 分页数据
                ];
                break;
            case 3:$model = $this->store;
                $model = $model->where('status',1);
                if($request->type){
                    switch($request->param('cate')){
                        case 2: $model = $model->where('category_id',16);break;
                        case 3: $model = $model->where('category_id',1);break;
                        case 4: $model = $model->where('category_id',18);break;
                        case 5: $model = $model->where('category_id',17);break;
                    }
                }
                if($request->param('name')){
                    $model = $model->where('name','like','%'.$request->param('name').'%');
                };
                if($request->param('state')==1){
                    $model = $model->where('is_top',1);
                };
                $res = $model->with('Tags'
                )->paginate($request->param('limit',10));

                $data = [
                    'total'     => $res->total(),         // 总记录数
                    'current_page'       => $res->currentPage(),   // 当前页码
                    'per_page'      => $res->listRows(),      // 每页记录数
                    'data'      => $res->items()          // 分页数据
                ];

                foreach($res->items() as $k=>$v){
                    $res->items()[$k]['tag'] = array_column($v['Tags']->toArray(),'name');
                };
                break;
            case 4:$model = $this->article;
                $model = $model->where(['status'=>1,'type'=>1])
                   ;
                if($request->param('name')){
                    $model = $model->where('title','like','%'.$request->param('name').'%');
                };

                $res = $model->paginate($request->param('limit',10));
                $data = [
                    'total'     => $res->total(),         // 总记录数
                    'current_page'       => $res->currentPage(),   // 当前页码
                    'per_page'      => $res->listRows(),      // 每页记录数
                    'data'      => $res->items()          // 分页数据
                ];
                break;

        }


        return json(['code'=>200,'msg'=>'获取成功','data'=>$data]);
    }

    /**
     * 获取教师列表
     * @param Request $request
     */
    public function teacher(Request $request)
    {
        $teacher = app('app\common\model\business\OrganTeacher');
        $list = $teacher->where(['store_id'=>$request->param('store_id'),'status'=>1])->select()->toArray();
        foreach($list as $k=>$v){
            $list[$k]['business'] = explode(',',$v['teaching']);
        }
        return json(['code'=>200,'msg'=>'获取成功','data'=>$list]);
    }
    /*机构详情*/
    public function store_detail(Request $request){
        $data = $this->store->with(['Tags','teacher'=>function($query){
            $query->where('status',1);
        }])->find($request->param('id'))->toArray();

        foreach($data['teacher'] as $k=>$v){
            $data['teacher'][$k]['business'] = explode(',',$v['teaching']);
        }
        $cate = $this->account->where(['type'=>2,'store_id'=>$request->param('id')])->column('category_id');

        $cate_list= $this->cate->where('type',2)->whereIn('id',$cate)->select();

        //$data['images'] = explode(',',$data['image']);

        $data['ad'] = array();
        if($data['image']){
            foreach($data['image'] as $k=>$v){

                $data['ad'][$k]['type'] = 1;
                $data['ad'][$k]['src'] = $v;
            }

        }
        $data['video'] !='' && array_push($data['ad'],['type'=>2,'src'=>$data['video']]);
        $data['tags'] = $data['Tags'];
        //$data['tag'] = array_column($data['tags'],'name');
        //实例化redis

        //判断是否已收藏
        if($this->collect->where(['type'=>3,'user_id'=>$request->user_id,'account_id'=>$data['id']])->find()){
            $data['is_collect'] = 1;
        }else{
            $data['is_collect'] = 0;
        }
        $this->admin = app('app\common\model\system\SystemAdmin');
        //$data['admin_id'] = $this->admin->where('store_id',$request->param('id'))->value('id');

        //商家账号
        //查询当前商家下所有客服账号
        $service = $this->admin->where(['store_id'=>$request->param('id'),'is_service'=>1])->column('id');

        //商家客服分流
        $to_platform = $this->chat_message->where(['from_id'=>$request->user_id,'type'=>3])->whereIn('from_id',$service)->find();

        if($to_platform){//如果之前有和商家聊天的记录，就继续和之前的客服聊天
            $data['admin_id'] = $to_platform->to_id;
        }else{ //如果没有就使用客服分流
            //查询所有平台客服账号

            $service_list = $this->admin
                ->where(['is_service'=>1,'store_id'=>$request->param('id')])
                ->field('id,reception_number')
                ->select()->toArray();

            $data['admin_id'] = $service_list[array_rand($service_list)]['id'];
        }

        $data['user_id'] = $request->user_id;

        $data['admin_avatar_url'] = 'http://bgt-param.oss-cn-hangzhou.aliyuncs.com/upload/20200721/49640565c5f8400385e46403877181269a063d0a.png';
        $data['user_avatar'] = $this->user->where('id',$request->user_id)->value('avatar_url');
        $data['user_name'] = $this->user->where('id',$request->user_id)->value('nick_name');
        return json(['code'=>200,'msg'=>'获取成功','data'=>['info'=>$data,'cate_list'=>$cate_list]]);
    }

    /*课程列表*/
    public function account(Request $request)
    {

        $model = $this->account;
        $model = $model->where(['status'=>1,'type'=>2,'store_id'=>$request->param('store_id')]);
        $request->param('category_id') && $model = $model->where('category_id',$request->param('category_id'));
        if($request->param('name')){
            $model = $model->where('title','like','%'.$request->get('name').'%');
        }

        $res = $model->paginate($request->param('limit',10));
        $data = [
            'total'     => $res->total(),         // 总记录数
            'current_page'       => $res->currentPage(),   // 当前页码
            'per_page'      => $res->listRows(),      // 每页记录数
            'data'      => $res->items()          // 分页数据
        ];
        return json(['code'=>200,'msg'=>'获取成功','data'=>$data]);
    }

    /*收藏*/
    public function collect(Request $request){
        //判断收藏类型
        try{
            if($request->param('type') ==1){//收藏机构
                //查询是否存在；如果存在则是取消收藏

                if($info = $this->collect->where(['type'=>3,'user_id'=>$request->user_id,'account_id'=>$request->param('id')])->find()){
                    $info->delete();
                }else{
                    $this->collect->save(['account_id'=>$request->param('id'),'type'=>3,'user_id'=>$request->user_id]);
                }

            } elseif($request->param('type')==2){//收线下藏课程

                //查询是否存在；如果存在则是取消收藏
                if($info = $this->collect->where(['user_id'=>$request->user_id,'type'=>1,'account_id'=>$request->param('id')])->find()){
                    $info->delete();
                }else{
                    $this->collect->save(['account_id'=>$request->param('id'),'type'=>1,'user_id'=>$request->user_id]);

                }

            }elseif($request->param('type')==3){//收网上藏课程

                //查询是否存在；如果存在则是取消收藏
                if($info = $this->collect->where(['user_id'=>$request->user_id,'type'=>2,'account_id'=>$request->param('id')])->find()){
                    $info->delete();
                }else{
                    $this->collect->save(['account_id'=>$request->param('id'),'type'=>2,'user_id'=>$request->user_id]);

                }

            }
            return json(['code'=>200,'msg'=>'成功']);
        }catch(\Exception $e){
            return json(['code'=>202,'msg'=>$e->getMessage()]);
        }

    }

    /*我的收藏*/
    public function my_collect(Request $request){

        if($request->param('type') ==1){
            $res = $this->collect->with(['stores'=>function($query){
                $query->with('Tags')->select();
            }])->where(['user_id'=>$request->user_id,'type'=>3])
                ->paginate($request->param('limit',10));

            if($res){
                foreach($res as $k=>$v){

                    $v['stores']['Tag'] && $res[$k]['stores']['tag'] = array_column($v['stores']['Tags']->toArray(),'name');
                };
            }

        }elseif($request->param('type') == 2){
            $res = $this->collect->with('account')
                ->where(['user_id'=>$request->user_id,'type'=>1])
                ->paginate($request->param('limit',10));
        }elseif($request->param('type') == 3){
            $res = $this->collect->with('net')->where(['user_id'=>$request->user_id,'type'=>2])
                ->paginate($request->param('limit',10));
        }
        if(!empty($res)){
            $data = [
                'total'     => $res->total(),         // 总记录数
                'current_page'       => $res->currentPage(),   // 当前页码
                'per_page'      => $res->listRows(),      // 每页记录数
                'data'      => $res->items()          // 分页数据
            ];
        }else{
            $data = [];
        }

        return json(['code'=>200,'msg'=>'获取成功','data'=>$data]);
    }

    /*线下课程详情*/
    public function account_detail(Request $request){
        $data = $this->account->with('items')->find($request->param('id'))->toArray();
        $this->account->where('id',$request->param('id'))->inc('click')->update();

        // $data['images'] = explode(',',$data['image']);
        foreach($data['image'] as $k=>$v){
            $data['ad'][$k]['type'] = 1;
            $data['ad'][$k]['src'] = $v;
        }
        $data['address'] = array_filter(explode(',',$data['address']));
        $data['subject'] = array_filter(explode(',',$data['subject']));

        $data['video'] !='' && array_push($data['ad'],['type'=>2,'src'=>$data['video']]);

        //判断是否已收藏
        if($this->collect->where(['user_id'=>$request->user_id,'type'=>1,'account_id'=>$request->param('id')])->find()){
            $data['is_collect'] = 1;
        }else{
            $data['is_collect'] = 0;
        }

        /*判断是否已购买  0:普通课程；1：网上课程*/

        if($this->user_net->where(['user_id'=>$request->user_id,'type'=>0,'net_id'=>$request->param('id')])->find()){

            $data['is_buyed'] = 1;
        }else{
            $data['is_buyed'] = 0;
        }


        //先判断是否存在团
        if($request->param('group_id') >0){//是否点进来别人的团，来参团
            $group = $this->group_master
                ->where(['id'=>$request->param('group_id')])->find();
            if($group->is_over ==0){ //未结束
                $data['other_number'] = $group->total_number-$group->join_number;
                $data['user_name'] = $this->user->find($group->user_id)->nick_name;

            }else if($group->is_over ==1){
                $data['is_over'] = 1;//拼团结束
                $data['other_number'] = 0;
            }else{
                return json(['code'=>202,'msg'=>'该团已结束']);
            }

            $data['is_group'] = 1;//1：是团购
            $data['group_id'] = $request->param('group_id',0);
            /*判断是否已经参过团*/

            if($order_info = $this->order->where(['user_id'=>$request->user_id,
                'type'=>0,'status'=>0,'goods_id'=>$request->param('id')])
                ->find()){
                $data['is_buyed'] = 1;
            }


        }/*是否进来的是自己开的团*/
        elseif($this->order->where(['user_id'=>$request->user_id,'type'=>0,'status'=>0,'goods_id'=>$request->param('id')])->find()){

            $data['is_buyed'] =1; //1：已支付

            $order = $this->order->where(['user_id'=>$request->user_id,'type'=>0,'status'=>0,'goods_id'=>$request->param('id')])->find();
            $data['is_group'] = 0;//1：是团购
            if($order){
                $group =$this->group_master
                    ->where(['is_over'=>0,'order_sn'=>$order->order_sn,'user_id'=>$request->user_id])->find();

                if($group){
                    $data['other_number'] = $group->total_number-$group->join_number; //团还剩人数
                    $data['user_name'] =$this->user->find($group->user_id)->nick_name;
                    $data['is_group'] = 1;//1：是团购
                    $data['group_id'] = $group->id;
                }
            }

        }/*单独购买*/
        elseif($this->order->where(['user_id'=>$request->user_id,'type'=>0,'goods_id'=>$request->param('id')])
            ->whereIn('status',[1,4,5])->find()){//单独购买
            $data['is_buyed'] =1; //1：已支付
            $data['is_group'] = 0;//1：是团购

        }

        $data['other_group_id'] = $request->param('group_id',0);
        /* dd(DB::table('group_master')
             ->where(['id'=>$data['other_group_id'],'is_over'=>0])->select());die;
         if($data['other_group_id'] && !DB::table('group_master')
                 ->where(['id'=>$data['other_group_id'],'is_over'=>0])->select()){
             $data['is_over'] = 1;//拼团结束
             $data['other_number'] = 0;
         }*/
        //dd($data['store_id']);
        $data['mobile'] = $this->store->find($data['store_id'])->phone;

        $this->admin = app('app\common\model\system\SystemAdmin');
        //$data['admin_id'] = $this->admin->where('store_id',$request->param('id'))->value('id');

        //商家账号
        //查询当前商家下所有客服账号
        $service = $this->admin->where(['store_id'=>$data['store_id'],'is_service'=>1])->column('id');

        //商家客服分流
        $to_platform = $this->chat_message->where(['from_id'=>$request->user_id,'type'=>3])->whereIn('from_id',$service)->find();

        if($to_platform){//如果之前有和商家聊天的记录，就继续和之前的客服聊天
            $data['admin_id'] = $to_platform->to_id;
        }else{ //如果没有就使用客服分流
            //查询所有平台客服账号
            $service_list = $this->admin
                ->where(['is_service'=>1,'store_id'=>$data['store_id']/*,'is_online'=>1*/])
                ->field('id,reception_number')
                ->select()->toArray();
            //dd($service_list);

            $data['admin_id'] = $service_list[array_rand($service_list)]['id'];
        }

        $data['user_id'] = $request->user_id;

        $data['admin_avatar_url'] = 'http://bgt-param.oss-cn-hangzhou.aliyuncs.com/upload/20200721/49640565c5f8400385e46403877181269a063d0a.png';
        $data['user_avatar'] = $this->user->where('id',$request->user_id)->value('avatar_url');
        $data['user_name'] = $this->user->where('id',$request->user_id)->value('nick_name');
        $data['service_phone'] = $this->store->find($data['store_id'])->service_phone;
        return json(['code'=>200,'msg'=>'获取成功','data'=>['info'=>$data]]);
    }

    /*我的已购网上课程*/

    public function my_net(Request $request){
        $res = $this->user_net->where(['user_id'=>$request->user_id,'type'=>1])
            ->with('nets')->paginate($request->param('limit',10));
        $data = [
            'total'     => $res->total(),         // 总记录数
            'current_page'       => $res->currentPage(),   // 当前页码
            'per_page'      => $res->listRows(),      // 每页记录数
            'data'      => $res->items()          // 分页数据
        ];
        return json(['code'=>200,'msg'=>'获取成功','data'=>$data]);
    }
    /*网上课程列表*/
    public function net_list(Request $request)
    {
        $model = $this->account->where('type',1);
        $Acc = $this->account;
        if($request->param('cate') ==2){ //已购
            $cate = $this->user_net->where(['user_id'=>$request->user_id,'type'=>1])->column('net_id');

            $model = $model->whereIn('id',$cate);

            /*1：全部；2：文化类；3：艺术类 4：成人教育；5：限时抢购*/

            switch($request->param('state',1)){
                case 1:/* $model = $model->where('category_id',1)*/;break;
                case 2:
                    $childs = $Acc->getAllNextId(1);
                    //获取所有子集
                    array_push($childs,1);

                    $model = $model->whereIn('category_id',$childs);
                    ;break;
                case 3:  $childs = $Acc->getAllNextId(16);
                    //获取所有子集
                    array_push($childs,16);

                    $model = $model->whereIn('category_id',$childs);
                    ;break;
                case 4:  $childs = $Acc->getAllNextId(18);
                    //获取所有子集
                    array_push($childs,18);

                    $model = $model->whereIn('category_id',$childs);
                    break;
                case 5: $model = $model->where('is_sale',1);break;

            }
            $res = $model->order('create_time','desc')->paginate($request->param('limit',10));
        }else{

            $model = $model->where('status',1);

            if($request->param('name')){
                $model = $model->where('title','like','%'.$request->get('name').'%');
            }
            /*1：全部；2：文化类；3：艺术类 4：成人教育；5：限时抢购*/

            switch($request->param('state',1)){
                case 1:/* $model = $model->where('category_id',1)*/;break;
                case 2:
                    $childs = $Acc->getAllNextId(1);
                    //获取所有子集
                    array_push($childs,1);

                    $model = $model->whereIn('category_id',$childs);
                    ;break;
                case 3:  $childs = $Acc->getAllNextId(16);
                    //获取所有子集
                    array_push($childs,16);

                    $model = $model->whereIn('category_id',$childs);
                    ;break;
                case 4:  $childs = $Acc->getAllNextId(18);
                    //获取所有子集
                    array_push($childs,18);

                    $model = $model->whereIn('category_id',$childs);
                    break;
                case 5: $model = $model->where('is_sale',1);break;
            }

            /*1：默认；2：最多学习；3：最新上架；4：积分兑换*/
            switch($request->param('type',1)){
                case 1: $res = $model->order('create_time','asc')->paginate($request->param('limit',10));break;
                case 2: $res = $model->order('sale','desc')->paginate($request->param('limit',10));break;
                case 3: $res = $model->order('create_time','desc')->paginate($request->param('limit',10));break;
                case 4: $res = $model->whereIn('type',[2,0])->order('create_time','desc')->paginate($request->param('limit',10));break;
            }

        }

        $data = [
            'total'     => $res->total(),         // 总记录数
            'current_page'       => $res->currentPage(),   // 当前页码
            'per_page'      => $res->listRows(),      // 每页记录数
            'data'      => $res->items()          // 分页数据
        ];
        return json(['code'=>200,'msg'=>'获取成功','data'=>$data]);
    }

    /*网上课程详情*/

    public function net_detail(Request $request){
        $data = $this->account->with(['items'=>function($query){
            $query->order('sort','asc');
        }])->find($request->param('id'));



        //判断是否已收藏

        if($this->collect->where(['user_id'=>$request->user_id,'type'=>2,'account_id'=>$request->param('id')])->find()){
            $data->is_collect = 1;
        }else{
            $data->is_collect = 0;
        }


        /*判断是否已购买  0:普通课程；1：网上课程*/

        if($this->user_net->where(['user_id'=>$request->user_id,'type'=>1,'net_id'=>$request->param('id')])->find()){
            $data->is_buyed = 1;
        }else{
            $data->is_buyed = 0;
        }


        /*获取观看进度*/
        foreach($data['items'] as $k=>$v){

            $see_info = $this->see_history->where(['user_id'=>$request->user_id,
                'account_id'=>$v['article_id'],'item_id'=>$v['id']])->find();

            if($see_info){
                $data['items'][$k]['is_seed'] = 1;
                $data['items'][$k]['time'] = $see_info->time;
                $data['items'][$k]['is_all'] = $see_info->is_all;
            }else{
                $data['items'][$k]['is_seed'] = 0;
                $data['items'][$k]['is_all'] = 0;
            }


            //查看开课时间
            if($data->start_time > date('Y-m-d H:i:s')){
                $data['items'][$k]['is_can'] = 0;
            } elseif($data->is_buyed ==1 && $data->start_time < date('Y-m-d H:i:s')){

                $data['items'][$k]['is_can'] = 1;

            }else{
                $data['items'][$k]['is_can'] = 0;

            }

        }



        return json(['code'=>200,'msg'=>'获取成功','data'=>['info'=>$data]]);
    }



}
